//管理用户数据相关
import {defineStore} from "pinia";
import {ref} from "vue";
import {loginAPI, loginOutAPI, userDetailAPI} from "@/apis/login.ts";

export const useUserStore=defineStore('user',()=>{

        //1.定义登录用户账号密码的数据的state
        const userInfo=ref({});
        // 2.定义登录用户的详细信息
        const userDetails=ref({});


        //2.定义获取接口数据的action函数
        const getUserInfo=async ({loginName,password}:{loginName:String,password:String})=>{
            const res=await loginAPI({loginName,password})
            userInfo.value=res.data;
            const rep=await userDetailAPI();
            userDetails.value=rep.data;
        }

        const clearUserInfo=async ()=>{
            await loginOutAPI();
           userInfo.value={};
           userDetails.value={};
        }

        //3.以对象的格式把state和action return
        return {
            userInfo,
            userDetails,
            getUserInfo,
            clearUserInfo,
        }
    },
    // pinia持久化配置，存入ls，true
    {
        persist:true,
    })



